package bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import oracle.jdbc.pool.OracleDataSource;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author BAGUS
 */
public class Pengiriman {

    private String kodePengiriman;
    private String waktuPengiriman;
    private String kodePemesanan;
    private String username;
    private double jumlahGasElpiji;
    private String jenisGasElpiji;
    private double hargaGasElpiji;
    private String status;
    private String kodeUpdate;
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:XE";
    String userid = "hr"; // gantikan dengan username anda
    String pass = "hr"; // gantikan dengan password anda

    public String getKodePengiriman() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select kodepengiriman from pengiriman order by kodepengiriman");
            String kode_pengiriman = "";
            Date now = new Date();
            SimpleDateFormat dateFormatterTanggal = new SimpleDateFormat("d");
            SimpleDateFormat dateFormatterBulan = new SimpleDateFormat("M");
            SimpleDateFormat dateFormatterTahun = new SimpleDateFormat("y");
            String tanggal = dateFormatterTanggal.format(now);
            String bulan = dateFormatterBulan.format(now);
            String tahun = dateFormatterTahun.format(now);
            String substring = tahun.substring(2);
            String kodeTanggal = tanggal + bulan + substring;
            while (resultSet.next()) {
                kode_pengiriman = resultSet.getString("kodepengiriman");
            }
            int no_pengiriman = 0;
            if ("".equals(kode_pengiriman)) {
                this.kodePengiriman = kodeTanggal + "301";
            } else {
                String data = null;
                if (kode_pengiriman.length() == 9) {
                    data = kode_pengiriman.substring(0, 6);
                } else if (kode_pengiriman.length() == 8) {
                    data = kode_pengiriman.substring(0, 5);
                }
                if (kodeTanggal.equals(data)) {
                    no_pengiriman = Integer.parseInt(kode_pengiriman) + 1;
                    this.kodePengiriman = String.valueOf(no_pengiriman);
                } else {
                    this.kodePengiriman = kodeTanggal + "301";
                }
            }
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            this.kodePengiriman = null;
        }
        return kodePengiriman;
    }

    public String getWaktuPengiriman() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d-M-y");
        this.waktuPengiriman = simpleDateFormat.format(date);
        return waktuPengiriman;
    }

    public String getKodePemesanan() {
        return kodePemesanan;
    }

    public void setKodePemesanan(String kodePemesanan) {
        this.kodePemesanan = kodePemesanan;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public double getJumlahGasElpiji() {
        return jumlahGasElpiji;
    }

    public void setJumlahGasElpiji(double jumlahGasElpiji) {
        this.jumlahGasElpiji = jumlahGasElpiji;
    }

    public String getJenisGasElpiji() {
        return jenisGasElpiji;
    }

    public void setJenisGasElpiji(String jenisGasElpiji) {
        this.jenisGasElpiji = jenisGasElpiji;
    }

    public double getHargaGasElpiji() {
        return hargaGasElpiji;
    }

    public void setHargaGasElpiji(double hargaGasElpiji) {
        this.hargaGasElpiji = hargaGasElpiji;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getStatus() {
        return status;
    }

    public String getKodeUpdate() {
        return kodeUpdate;
    }

    public void setKodeUpdate(String kodeUpdate) {
        this.kodeUpdate = kodeUpdate;
    }

    public void insertPengiriman() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            if ("belum".equals(getStatus())) {
                ResultSet resultSet = statement.executeQuery("select jenisgaselpiji "
                        + "from pemesanan "
                        + "where kodepemesanan = '" + getKodePemesanan() + "'");
                String jenisgaselpiji1 = "";
                while (resultSet.next()) {
                    jenisgaselpiji1 = resultSet.getString("jenisgaselpiji");
                }
                statement.executeQuery("insert into pengiriman values ('"
                        + getKodePengiriman() + "','"
                        + getWaktuPengiriman() + "','"
                        + getKodePemesanan() + "','"
                        + getUsername() + "',"
                        + getJumlahGasElpiji() + ",'"
                        + jenisgaselpiji1 + "',"
                        + getHargaGasElpiji() + ",'"
                        + getStatus() + "')");
            } else {
                statement.executeUpdate("update pengiriman"
                        + " set status = 'terkirim',"
                        + " jumlahgaselpiji = " + getJumlahGasElpiji()
                        + " where kodepengiriman = '" + getKodeUpdate() + "'");


                Pemesanan pemesanan = new Pemesanan();
                ResultSet resultSet1 = statement.executeQuery("select kodepemesanan from pengiriman"
                        + " where kodepengiriman = '" + getKodeUpdate() + "'");
                String kodepemesanan1 = "";
                while (resultSet1.next()) {
                    kodepemesanan1 = resultSet1.getString("kodepemesanan");
                }
                pemesanan.setKodeUpdate(kodepemesanan1);
                pemesanan.setStatus("terkirim");
                pemesanan.insertPemesanan();


                GasElpiji gasElpiji = new GasElpiji();
                ResultSet resultSet = statement.executeQuery("select jenisgaselpiji from pengiriman"
                        + " where kodepengiriman = '" + getKodeUpdate() + "'");
                String jenisgaselpiji1 = "";
                while (resultSet.next()) {
                    jenisgaselpiji1 = resultSet.getString("jenisgaselpiji");
                }
                gasElpiji.setJenis(jenisgaselpiji1);
                gasElpiji.setJumlah(getJumlahGasElpiji());
                gasElpiji.kurangGasElpiji();
            }
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
        }
    }

    public String tampilNamaPemesan() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select username from pemesanan where kodepemesanan = '" + getKodePemesanan() + "'");
            String username1 = "";
            while (resultSet.next()) {
                username1 = resultSet.getString("username");
            }
            ResultSet resultSet2 = statement.executeQuery("select namauser from usergaselpiji where username = '" + username1 + "'");
            String namaUser = "";
            while (resultSet2.next()) {
                namaUser = resultSet2.getString("namauser");
            }
            return namaUser;
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            return null;
        }
    }

    public String tampilJenisGasElpiji() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select jenisgaselpiji from pemesanan where kodepemesanan = '" + getKodePemesanan() + "'");
            String jenisgaselpiji1 = "";
            while (resultSet.next()) {
                jenisgaselpiji1 = resultSet.getString("jenisgaselpiji");
            }
            return jenisgaselpiji1;
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            return null;
        }
    }

    public String tampilJenisPengiriman(String kodepengiriman) {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select jenisgaselpiji from pengiriman where kodepengiriman = '" + kodepengiriman + "'");
            String jenisgaselpiji1 = "";
            while (resultSet.next()) {
                jenisgaselpiji1 = resultSet.getString("jenisgaselpiji");
            }
            return jenisgaselpiji1;
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            return null;
        }
    }

    public ResultSet tampilPengiriman() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from pengiriman");
            return resultSet;
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            return null;
        }
    }

    public ResultSet tampilKodePengiriman() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select kodepengiriman from pengiriman where status = 'belum'");
            return resultSet;
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            return null;
        }
    }
}
